/**
 * @author		Sergey Suzdalskiy
 * @e-mail:		sergey.suzdalskiy@gmail.com
 */
package ua.kiev.gcore.ui.controls
{
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	
	

	public class TabBox extends Sprite
	{
		public static const V_TYPE:int = 1;
		public static const H_TYPE:int = 2;
		
		private var tabButtons:Array;
		private var tabContainer:Box;		
		private var target:Box;
		
		private var tabTargets:Vector.<TabSprite>;
		private var currentTab:String;
		
		public function TabBox(targetContainer:Box, type:int)
		{			 
			target = targetContainer;
			tabButtons = new Array();
			tabTargets = new Vector.<TabSprite>();
			switch(type){
				case H_TYPE:
					tabContainer = new HBox();
					break;
				case V_TYPE:
					tabContainer = new VBox();
					break;
				default:
					tabContainer = new Box();
					break;
			}
			addChild(tabContainer);
		}
		public function globalTick():void{
			if(currentTab){
				currentTabSprite.globalTick();
			}
		}
		public function addTab(page:TabSprite, tabButton:ButtonSelect):void{
			//var tabButton:ButtonSelect = new ButtonSelect(tabTargets.length, buttonLabel, buttonBackGround, selectTab, autoSizeByText);
			tabButton.Listener = selectTabHandler
			
			tabButtons.push(tabButton);			
			tabTargets.push(page);
			
			tabContainer.addChild(tabButton);						
		}
		private function selectTabHandler(button:Button):void{
			currentTab = button.UID;
			selectTab(currentTab);
		}
		public function selectTab(uid:String):void{
			currentTab = uid;
			target.clear();
			if(currentTab){
				for each( var tab:TabSprite in tabTargets){
					if(tab.UID == currentTab){
						target.addChild(tab);
						tab.update();
						break;
					}
				}				
				for each(var tabButton:ButtonSelect in tabButtons){
					if(tabButton.UID == currentTab){
						tabButton.Selected = true;					
					}else{
						tabButton.Selected = false;
					}
				}
			}
		}
		public function get currentTabSprite():TabSprite{
			for each( var tab:TabSprite in tabTargets){
				if(tab.UID == currentTab){
					return tab;
				}
			}	
			return null;
		}
	}
}